package com.example.springboot2.CommonHelper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Collection;
@Component
@Slf4j
public class RedisHelper {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public boolean set(final String key, Object value) {
        try{
            redisTemplate.opsForValue().set(key,value);
            return true;
        }catch (Exception e){
            //log.error("redis set value exception:{}",e);
            return false;
        }
    }

    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }
    public void remove(String ...keys) {
        if(keys!=null&&keys.length>0) {
            redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(keys));
        }
    }
    public Object get(String key) {
        try
        {
            return  key == null ? null : redisTemplate.opsForValue().get(key);
        }
        catch (Exception ex)
        {
            return  null;
        }

    }
}
